﻿using Foxit.PDF;
using Foxit.PDF.IO;
using Foxit.PDF.PageElements;

namespace Sustainalytics.Reporting.Framework
{
    public class BulletScore : PageElement
    {
        public float Left { get; set; }
        public float Top { get; set; }
        public float Radius { get; set; }
        public string Value { get; set; }
        public Font ValueFont { get; set; }
        public float ValueFontSize { get; set; }

        private Group Elements()
        {
            const float borderWidth = 0.1f;

            var elements = new Group();
            
            elements.Add(
                new Circle(Left + Radius, Top+Radius, Radius, Colors.GreenBulletColor, Colors.GreenBulletColor, borderWidth, LineStyle.Solid));

            var labelWidth = Radius * 2;
            var currentfontSize = ValueFontSize;
            while (ValueFont.GetTextWidth(Value, currentfontSize) >= labelWidth-3f)
            {
                currentfontSize -= 0.5f;
            }
            var labelHeigth = currentfontSize + 2f;
            var labelTopOffset = Top + (Radius * 2 - currentfontSize) / 2 + 1f;

            elements.Add(new Label(Value,
                Left, labelTopOffset, labelWidth, labelHeigth, ValueFont, currentfontSize, TextAlign.Center, RgbColor.White));

            return elements;
        }

        public override void Draw(PageWriter writer)
        {
            var elements = Elements();

            writer.Write_q_(true);
            elements.Draw(writer);
            writer.Write_Q(true);
        }
    }
}
